package com.artisan.provider.controller;

import com.artisan.provider.contanent.Contanents;
import com.artisan.provider.covtransformation.CovIntegralTransformation;
import com.artisan.provider.domain.ZgIntegral;
import com.artisan.provider.dto.ZgIntegralDto;
import com.artisan.provider.service.ZgIntegralService;
import com.artisan.provider.utils.Request;
import com.artisan.provider.vo.ZgIntegralVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ZgIntegralController {

    @Autowired
    ZgIntegralService zgIntegralService;



    /***
     * 查询所有优惠券信息
     * @param zgIntegralDto
     * @param page
     * @param limit
     * @return
     */
    @PostMapping(value = "/api/integral/find/all/{page}/{limit}")
    public Request zgIntegralFindAll(@RequestBody(required = false) ZgIntegralDto zgIntegralDto,
                                        @PathVariable(value = "page",required = false) int page,
                                        @PathVariable(value = "limit",required = false)int limit
                                        ){

        List<ZgIntegral> zgIntegrals = zgIntegralService.findAll(zgIntegralDto,page,limit);
        List<ZgIntegralVo> zgIntegralVos = CovIntegralTransformation.INSTANCE.zgIntegralVos(zgIntegrals);
        if (zgIntegrals != null){
            return new Request(Contanents.DATA_SUCCESS,true,zgIntegralVos);
        }else{
            return new Request(Contanents.DATA_FAILED,false,new ArrayList<>());
        }

    }

    /**
     * 数据的添加业务
     * @param zgIntegralDto
     * @return
     */
    @PostMapping(value = "/api/integral/add")
    public Request zgIntegralAdd(@RequestBody ZgIntegralDto zgIntegralDto){
        ZgIntegral zgIntegral = CovIntegralTransformation.INSTANCE.zgIntegral(zgIntegralDto);
        if(zgIntegralService.zgIntegralAdd(zgIntegral) == Contanents.DATA_ADD_REQUEST_ONE){
            return new Request(Contanents.DATA_SUCCESS,true,Contanents.DATA_ADD_REQUEST_ONE);
        }else{
            return new Request(Contanents.DATA_FAILED,false,Contanents.DATA_ADD_REQUEST_ZERO);
        }
    }

    /**
     * 数据的修改业务
     * @param zgIntegralDto
     * @return
     */
    @PutMapping(value = "/api/integral/update")
    public Request zgIntegralUpdate(@RequestBody ZgIntegralDto zgIntegralDto){
        ZgIntegral zgIntegral = CovIntegralTransformation.INSTANCE.zgIntegral(zgIntegralDto);
        if(zgIntegralService.zgIntegralUpdate(zgIntegral) == Contanents.DATA_ADD_REQUEST_ONE){
            return new Request(Contanents.DATA_SUCCESS,true,Contanents.DATA_ADD_REQUEST_ONE);
        }else{
            return new Request(Contanents.DATA_FAILED,false,Contanents.DATA_ADD_REQUEST_ZERO);
        }
    }

    /**
     * 数据的删除业务
     * @param id
     * @return
     */
    @DeleteMapping(value = "/api/integral/delete/{id}")
    public Request zgIntegralDelete(@PathVariable(value = "id")int id){

        if(zgIntegralService.zgIntegralDelete(id) == Contanents.DATA_ADD_REQUEST_ONE){
            return new Request(Contanents.DATA_SUCCESS,true,Contanents.DATA_ADD_REQUEST_ONE);
        }else{
            return new Request(Contanents.DATA_FAILED,false,Contanents.DATA_ADD_REQUEST_ZERO);
        }
    }

}
